//初始化列表数组
var List = [];
var startDate = '';
var endDate = '';
var editUserId = 0;
//分页查询
var limit = 10;// 每页多少条
var currPage = 1;// 当前页
var allPage = 1;// 总页数
var count = 0;// 默认共多少条数据
//全局变量
var name = '';
var fname = '';
var networkName = '';
var neId = 0;

var one = 0;
var tow = 0;
var three = 0;




function doSend(){
    name = val('name');
    fname = val('fname');
	getAdminlist();
}

getAdminlist();
function getAdminlist(){
	console.log(startDate);
	console.log(endDate);


	$.ajax({
		url:ajax_host+'selectNetWorkINFO.act',
		dataType : 'JSON',
		data:{
            name : name,
            fname : fname,
			startpage : currPage,
			limitpage : limit
		},
		success:function(resp){
			
			if(resp.success){
				List = resp.list;
				console.log("查询后列表："+List);
				renderTable(resp.list);

				// 总页数=记录数/每页记录
				count = resp.count;
				console.log('总记录数:'+count)
				allPage=count%limit==0?(count/limit):(parseInt(count/limit+1));
				console.log('总页数:'+allPage);
				$("#pager").html(currPage+"/"+allPage);
			}else{
				layer.alert(resp.errmsg);
			}
        }
	})
}


function renderTable(arr){
	var str = `<tr>
		<th>网点(市)</th>
		<th>网点(区县)</th>
		<th>快件总数</th>
		<th>问题件总数</th>
		<th>退单件总数</th>
		<th>网点总收益</th>
        <th>网点经度</th>
        <th>网点维度</th>
        <th>网点名</th>
        <th>网点详细地址</th>
        <th>操作</th>
	</tr>`;
	for(var i = 0;i<arr.length;i++){
		var a = arr[i];
		str += 
		   `<tr>
			<td>${a.nfname}</td>
            <td>${a.nAname}</td>
            <td>${a.eSun}</td>
            <td>${a.pSun}</td>
			<td>${a.bSun}</td>
			<td>${a.naMoney}</td>
            <td>${a.nLong}</td>
            <td>${a.nLat}</td>
            <td>${a.netName}</td>
            <td>${a.detail}</td>
			<td>
			<a href = "###" onclick="doUpdate(${a.neId})">修改网点名</a>
			<a href = "###" onclick="del(${a.neId})">删除</a>
			
			</td>
		</tr>`;
		
	}
	html('table',str); 
}

function pagenext() {
	if (currPage + 1 > allPage) {
		layer.alert("当前已经是最后一页");
		return;
	}
	
	currPage = currPage + 1;
	getAdminlist();
};

function pageback() {
	if (currPage - 1 == 0) {
		layer.alert("当前已经是第一页");
		return;
	}
	
	currPage = currPage - 1;
	getAdminlist();
};


function doUpdate(id){
    neId = id;
    console.log('获得修改的id'+neId)
    $('#doUpdate').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
        backdrop:'static'//背景遮罩
	});
	

}
function confirm2(){
    $('#doUpdate').modal('hide', 'fit');
    networkName = val('networkName');
    $.ajax({
		url:ajax_host+'netWorkNameEdit.act',
		type : 'post',
		dataType:'JSON',
		data:{
			networkName : networkName,
			neId : neId
		},
		success:function(resp){
			
			if(resp.success){
				//关闭遮罩
				$('#doUpdate').modal('hide', 'fit');
				layer.alert('修改成功');
				getAdminlist();
			}else{
				$('#doUpdate').modal('hide', 'fit');
				layer.alert(resp.errmsg);
			}
		}
})
}

function doAdd(){
    $('#doAdd').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
        backdrop:'static'//背景遮罩
	});
	

}
getaddressone();
function getaddressone(){
    console.log('开始获取省级列表')
    $.ajax({
		url : ajax_host +'getAdminaddressone.act',
		dataType:'JSON',
		
		success:function(resp){
			if(resp.success){
                addressonelist = resp.list;
                
				console.log("列表："+addressonelist);
				  renderAddressOne(addressonelist)
			}
		}
	})
}
function renderAddressOne(arr){
    var str = '<option value="0">请选择</option>';
    for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressOne',str);
}
function change(){
    one = val('addressOne');
    console.log('one'+one);
    $.ajax({
		url : ajax_host +'getAdminaddressTow.act',
		dataType:'JSON',
		data:{
			fid : one
		},
		success:function(resp){		
			if(resp.success){
            addressTowlist = resp.list;
			renderAddressTow(addressTowlist);
		}else{
			layer.alert(resp.errmsg);
		}
		}
	})
}
function renderAddressTow(arr){
	var str = '<option value="0">请选择</option>';
	for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressTwo',str);
}
function change2(){
     tow = val('addressTwo');
    console.log('tow'+tow);
    $.ajax({
		url : ajax_host +'getAdminaddressThree.act',
		dataType:'JSON',
		data:{
			fid : tow
		},
		success:function(resp){		
			if(resp.success){
             addressThreelist = resp.list;
			renderAddressThree(addressThreelist);
		}else{
			layer.alert(resp.errmsg);
		}
		}
	})
}
function renderAddressThree(arr){
	var str = '<option value="0">请选择</option>';
	for(var i = 0; i<arr.length;i++){
		str+=`<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
	}
	html('addressThree',str);
}


function change3(){
     three = val('addressThree');
    console.log('three'+three);
    
}
function confirm(){
    $('#doAdd').modal('hide', 'fit');
    console.log('---------'+one);
    console.log('---------'+tow);
    console.log('---------'+three);
    var netName = $('#netName').val();
    var detail = $('#detail').val();
    console.log('---------'+netName);
    console.log('---------'+detail);
    $.ajax({
		url:ajax_host+'addNetwork.act',
		type : 'post',
		dataType:'JSON',
		data:{
			addressOne : one,
			addressTwo : tow,
			addressThree : three,
			netName : netName,
			detail : detail
			
		},
		
		success:function(resp){
			if(resp.success){
				layer.alert('添加成功');
				
				getAdminlist();
				$('#doAdd').modal('hide', 'fit');
			}else{
				layer.alert(resp.errmsg);
				$('#doAdd').modal('hide', 'fit');
			}
		}
	})
}

//给删除按钮挂事件
function del(id){
	console.log('列表删除'); 
	layer.confirm("确定删除该网点吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

    neId = id;
	$.ajax({
		url:ajax_host+'netWorkDel.act',
		type:'post',
		dataType:'JSON',
		data:{
			neId : neId
		},
	success:function(resp){
		if(resp.success){
			//删除数组的元素
			layer.alert('删除成功');
			
			getAdminlist();
			
		}else{
			layer.alert(resp.errmsg);
		}
		
	}
	})


   });
		
	
}